package com.person.jc.service.base.equip;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.common.SQLSpell;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

public class BdEquipmentService extends Service {
	
	public final static BdEquipmentService me = new BdEquipmentService();
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select equ_id,equ_name,equ_dept,equ_model,equ_code," +
				"equ_manufactor,equ_cycle,cert_code,measure_scope,equ_memo,remark,order_no,device_type, date_format(date_add(equ_date, INTERVAL equ_cycle MONTH), '%Y-%m-%d') next_equ_date,"+
				"date_format(equ_date, '%Y-%m-%d') equ_date,datediff(date_add(equ_date, INTERVAL equ_cycle MONTH) , now()) diff_day, " +
				"("+SQLSpell.kindName("device_type", "t.device_type")+") device_type_name " +
				"from bd_equipment t where if_valid = 1");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("equName")))){
			sql.append(" and equ_name like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("equDept")))){
			sql.append(" and equ_dept like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("equManufactor")))){
			sql.append(" and equ_manufactor like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("equModel")))){
			sql.append(" and equ_model like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("equCode")))){
			sql.append(" and equ_code like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("certCode")))){
			sql.append(" and cert_code like '%"+ temp +"%'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("equId")))){
			sql.append(" and equ_id = '"+ temp +"'");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deviceType")))){
			sql.append(" and device_type = '"+ temp +"'");
		}

		if(Utils.isNotEmpty(temp = String.valueOf(map.get("equIds")))){
			sql.append(" and equ_id in ("+ temp +")");

		}

		sql.append(" order by device_type, order_no, diff_day, equ_name ");
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void save(Map<String,Object> map){
		Record equip = new Record();
		equip.set("equ_name", map.get("equName"));
		equip.set("equ_dept", map.get("equDept"));
		equip.set("equ_model", map.get("equModel"));
		equip.set("equ_code", map.get("equCode"));
		equip.set("equ_manufactor", map.get("equManufactor"));
		equip.set("equ_date", map.get("equDate"));
		equip.set("equ_cycle", map.get("equCycle"));
		equip.set("cert_code", map.get("certCode"));
		equip.set("measure_scope", map.get("measureScope"));
		equip.set("equ_memo", map.get("equMemo"));
		equip.set("device_type", map.get("deviceType"));
		equip.set("remark", map.get("remark"));
		
		Object orderNo = Utils.isNotEmpty(String.valueOf(map.get("orderNo")))?map.get("orderNo"):null;
		equip.set("order_no", orderNo);
		Db.save("bd_equipment", equip);
		super.setCRUD("计量器具管理", "C", "新增计量器具："+map.get("equName"));
	}
	
	public void update(Map<String,Object> map){
		Record equip = new Record();
		equip.set("equ_id", map.get("equId"));
		equip.set("equ_name", map.get("equName"));
		equip.set("equ_dept", map.get("equDept"));
		equip.set("equ_model", map.get("equModel"));
		equip.set("equ_code", map.get("equCode"));
		equip.set("equ_manufactor", map.get("equManufactor"));
		equip.set("equ_date", map.get("equDate"));
		equip.set("equ_cycle", map.get("equCycle"));
		equip.set("cert_code", map.get("certCode"));
		equip.set("measure_scope", map.get("measureScope"));
		equip.set("equ_memo", map.get("equMemo"));
		equip.set("device_type", map.get("deviceType"));
		equip.set("remark", map.get("remark"));
		
		Object orderNo = Utils.isNotEmpty(String.valueOf(map.get("orderNo")))?map.get("orderNo"):null;
		equip.set("order_no", orderNo);
		Db.update("bd_equipment", "equ_id",equip);
		super.setCRUD("计量器具管理", "U", "修改计量器具，编号："+map.get("equId"));
	}
	
	public void delete(String equId){
		String sql = " update bd_equipment set if_valid = 0 where equ_id = ? ";
		Db.update(sql, equId);
		super.setCRUD("计量器具管理", "D", "删除计量器具，编号："+equId);
	}
	
	public long checkName(String equCode, String equId){
		String sql = "select count(1) from bd_equipment where equ_code = ? and if_valid <> 0 ";
		if(Utils.isNotEmpty(equId)){
			sql += " and equ_id <> " + equId;
		}
		return Db.queryLong(sql, equCode);
	}
}
